package reflection.exercise;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-04-17
 * Time:15:07
 */
public class Exercise1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<?> cls = Class.forName("reflection.exercise.PrivateTest");
        Object o = cls.getDeclaredConstructor().newInstance();
        Field name = cls.getDeclaredField("name");
        name.setAccessible(true);
        name.set(o,"Kitty");
        Method getName = cls.getDeclaredMethod("getName");
        Object invoke = getName.invoke(o);
        System.out.println(invoke);//Kitty
    }
}
class PrivateTest{
    private String name = "hellokitty";
    public String getName(){
        return name;
    }
}